package com.huawei.appgallery.forum.cards.widget;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.TypefaceSpan;
import android.util.AttributeSet;
import android.widget.TextView;
import androidx.annotation.Nullable;
import com.huawei.appgallery.foundation.ui.css.CSSRule;
import com.huawei.appgallery.foundation.ui.css.CSSViewProxy;
import com.huawei.appgallery.foundation.ui.css.RenderListener;
import com.huawei.appgallery.foundation.ui.css.adapter.CSSPropertyName;
import com.huawei.appgallery.foundation.ui.css.adapter.type.CSSMonoColor;
import com.huawei.appgallery.foundation.ui.css.adapter.type.CSSValue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import o.bqx;
import o.bsy.d;
import o.dof;
import o.erq;
import o.evf;

public class HighLightTextView extends TextView implements RenderListener {
    private int ʼ;
    private int ʽ;
    private String ˊ;
    private boolean ˋ;
    private boolean ˎ = false;
    private String ˏ;
    private int ॱ;

    public HighLightTextView(Context context) {
        super(context);
    }

    public HighLightTextView(Context context, @Nullable AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public HighLightTextView(Context context, @Nullable AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
    }

    public void setTextHighLightColor(String str) {
        this.ॱ = Color.parseColor(str);
    }

    public void setTextHighLightColor(int i) {
        this.ॱ = getResources().getColor(i);
    }

    public void setTextHighLightTypeface(String str) {
        this.ˏ = str;
    }

    public void setTextToHighLight(String str) {
        this.ˊ = str;
    }

    public void setCaseInSensitive(boolean z) {
        this.ˋ = z;
    }

    public void ॱ() {
        if (!dof.ॱ(getText().toString()) && !dof.ॱ(this.ˊ)) {
            if (this.ॱ != 0 || dof.ॱ(this.ˏ)) {
                CharSequence spannableStringBuilder = new SpannableStringBuilder(getText());
                try {
                    Pattern compile;
                    if (this.ˋ) {
                        compile = Pattern.compile(Pattern.quote(this.ˊ), 2);
                    } else {
                        compile = Pattern.compile(Pattern.quote(this.ˊ));
                    }
                    Matcher matcher = compile.matcher(getText());
                    while (matcher.find()) {
                        if (this.ॱ != 0) {
                            spannableStringBuilder.setSpan(new ForegroundColorSpan(this.ॱ), matcher.start(), matcher.end(), 33);
                        }
                        if (!dof.ॱ(this.ˏ)) {
                            spannableStringBuilder.setSpan(new TypefaceSpan(this.ˏ), matcher.start(), matcher.end(), 33);
                        }
                    }
                } catch (Exception e) {
                    bqx.ˋ("HighLightTextView", "match search key error.");
                }
                setText(spannableStringBuilder);
            }
        }
    }

    public boolean onRenderReady(CSSViewProxy cSSViewProxy) {
        CSSRule rule = cSSViewProxy.getRule();
        if (rule.getStyleDeclaration() == null) {
            return false;
        }
        CSSValue propertyValue = rule.getStyleDeclaration().getPropertyValue(CSSPropertyName.FONT_COLOR);
        if (!(propertyValue instanceof CSSMonoColor)) {
            return false;
        }
        int color = ((CSSMonoColor) propertyValue).getColor();
        this.ˎ = true;
        this.ʽ = color;
        this.ʼ = erq.ˊ(color, 0.5f);
        return true;
    }

    public void setUnReadTextColor() {
        if (this.ˎ) {
            setTextColor(this.ʽ);
        } else {
            setTextColor(evf.ॱ(getContext(), d.ˋ));
        }
    }

    public void setReadedTextColor() {
        if (this.ˎ) {
            setTextColor(this.ʼ);
        } else {
            setTextColor(evf.ॱ(getContext(), d.ˋ));
        }
    }

    public void setUnReadTextColor(int i) {
        setTextColor(i);
    }

    public void setReadedTextColor(int i) {
        setTextColor(i);
    }
}
